<template>
  <div>
    <div style="margin-bottom: 5px">
      <el-input v-model="rentgoodsName"  placeholder="请输入物品名" suffix-icon="el-icon-search" style="width: 300px"
                @keyup.enter.native="loadPost"></el-input>
      <el-select v-model="cId" filterable placeholder="请选择社团" style="margin-left: 5px;width: 300px" @change="loadPost" v-if="user.roleId!=1">
        <el-option
            v-for="item in cNameData"
            :key="item.cid"
            :label="item.cname"
            :value="item.cid">
        </el-option>
      </el-select>
      <el-button style="margin-left: 5px" type="primary" icon="el-icon-search" @click="loadPost">查询</el-button>
      <el-button style="margin-left: 5px" type="success" icon='el-icon-refresh-left' @click="reset">重置</el-button>
      <el-button style="margin-left: 5px" type="warning" icon='el-icon-circle-plus-outline' @click="add" v-if="user.roleId!=2">登记</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%"
              :header-cell-style="{background: '#f3f6fd', color: '#211e1e'}"

    >
      <el-table-column prop="rentgoodsId" label="ID" min-width="40%" align="center">
      </el-table-column>
      <el-table-column prop="rentgoodsName" label="物品名" min-width="100%" align="center">
      </el-table-column>
      <el-table-column prop="rentgoodsCount" label="物品数量" min-width="100%" align="center">
      </el-table-column>
      <el-table-column prop="cname" label="所属社团" min-width="100%" align="center">
      </el-table-column>
      <el-table-column prop="rentgoodsRemark" label="物品备注" min-width="100%" align="center">
      </el-table-column>
      <el-table-column prop="operate" label="操作" min-width="80%" align="center" v-if="user.roleId!=2">
        <template v-slot="scope">
          <el-button type="text" icon="el-icon-edit" size="medium" @click="mod(scope.row)" >编辑</el-button>
          <el-button type="text" icon="el-icon-circle-plus-outline" size="medium" @click="add1(scope.row)"style="margin-left: 5px" >加仓</el-button>
          <el-popconfirm
              title="确定删除吗？"
              @confirm="del(scope.row.rentgoodsId)"
              style="margin-left: 5px"
          >
            <el-button slot="reference" type="text" icon="el-icon-delete" size="medium" >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
      <el-table-column prop="operate" label="操作" min-width="80%" align="center" v-if="user.roleId==2">
        <template v-slot="scope">
          <el-button type="text" icon="el-icon-circle-plus-outline" size="medium" @click="rent(scope.row)"style="margin-left: 5px" >租借</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="物品名" prop="rentgoodsName">
          <el-col :span="20">
            <el-input v-model="form.rentgoodsName" ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="物品数量" prop="rentgoodsCount">
          <el-col :span="20">
            <el-input v-model="form.rentgoodsCount" ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="社团" prop="cid" v-if="user.roleId==0">
          <el-col :span="20">
            <el-select v-model="form.cid" filterable placeholder="请选择社团" >
              <el-option
                  v-for="item in cNameData"
                  :key="item.cid"
                  :label="item.cname"
                  :value="item.cid">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="物品备注" prop="rentgoodsRemark" >
          <el-col :span="20">
            <el-input v-model="form.rentgoodsRemark" type="textarea"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" >
    <el-button @click="centerDialogVisible=false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog
        title="提示"
        :visible.sync="addDialogVisible"
        width="30%"
        center>
      <el-form ref="addform"  :model="addform" label-width="80px">
        <el-form-item label="物品名" prop="rentgoodsName">
          <el-col :span="20">
            <el-input v-model="addform.rentgoodsName" disabled></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="物品数量" prop="rentgoodsCount">
          <el-col :span="20">
            <el-input v-model="addform.rentgoodsCount" ></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" >
    <el-button @click="addDialogVisible=false">取 消</el-button>
    <el-button type="primary" @click="addsave">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog
        title="提示"
        :visible.sync="rentDialogVisible"
        width="30%"
        center>
      <el-form ref="useform"  :model="rentform" label-width="80px">
        <el-form-item label="物品名" prop="rentgoodsName">
          <el-col :span="20">
            <el-input v-model="rentform.rentgoodsName" disabled></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="物品数量" prop="usegoodsCount">
          <el-col :span="20">
            <el-input v-model="rentform.rentgoodsCount" ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="申请理由" prop="useinfo" >
          <el-col :span="20">
            <el-input v-model="rentform.rentinfo" type="textarea"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" >
    <el-button @click="rentDialogVisible=false">取 消</el-button>
    <el-button type="primary" @click="rentsave">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "RentgoodsManage",
  components: {
  },

  data(){
    return {
      user: JSON.parse(sessionStorage.getItem('CurUser')),
      disabled_index:false,
      addDialogVisible:false,
      rentDialogVisible:false,
      tableData: [],
      cNameData:[],
      pageSize:10,
      pageNum:1,
      total:0,
      rentgoodsName:'',
      cId:'',
      centerDialogVisible:false,
      form:{
        rentgoodsId:'',
        rentgoodsName:'',
        rentgoodsCount:'',
        rentgoodsRemark:'',
        cid:''
      },
      addform:{
        rentgoodsName:'',
        rentgoodsCount:'',
      },
      rentform:{
        rentgoodsId:'',
        rentgoodsCount:'',
        rentgoodsRemark:'',
        rentinfo:'',
        userId:''
      },
      rules:{
        rentgoodsName: [
          { required: true, message: '请输入社团名字', trigger: 'blur' },
        ],
        rentgoodsCount: [
          { required: true, message: '请备注', trigger: 'blur' },
        ],
        rentgoodsRemark: [
          { required: true, message: '请备注', trigger: 'blur' },
        ],
        cid: [
          { required: true, message: '请备注', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    // this.loadGet();
    this.loadPost();
    this.loadCname();
  },
  methods:{
    rentsave(){
      this.$axios.post(this.$httpUrl+'/rentgoods/rentsave',this.rentform).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          });
          this.rentDialogVisible=false
          this.rentform.usegoodsCount = ''
        }else{
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    rent(row){
      console.log(row)
      this.rentDialogVisible=true
        this.rentform.rentgoodsId= row.rentgoodsId
        this.rentform.rentgoodsName= row.rentgoodsName
        this.rentform.rentinfo = row.rentinfo
        this.rentform.userId = this.user.userId
    },
    loadCname(){
      this.$axios.get(this.$httpUrl+'/community/list').then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.cNameData=res.data
          //过滤管理员
          let id = this.cNameData.findIndex(item => {
            if (item.cid == '1') {
              return true
            }
          })
          this.cNameData.splice(id, 1)
        }else{
          alert("获取数据失败")
        }
      })
    },
    resetForm(){
      this.$refs.form.resetFields();
    },
    mod(row){
      console.log(row)
      this.centerDialogVisible=true
      this.$nextTick(()=>{
        this.form.rentgoodsId= row.rentgoodsId
        this.form.rentgoodsName= row.rentgoodsName
        this.form.rentgoodsCount= row.rentgoodsCount
        this.form.rentgoodsRemark= row.rentgoodsRemark
        this.form.cid= row.cid
        this.disabled_index = true
      })

    },
    del(id){
      console.log(id)
      this.$axios.get(this.$httpUrl+'/rentgoods/delete?rentgoodsId='+id).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible=false
          this.loadPost()
        }else{
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    loadGet(){
      this.$axios.get(this.$httpUrl+'/rentgoods/list').then(res=>res.data).then(res=>{
        console.log(res)
      })
    },
    loadPost(){
      if (this.user.roleId != '1'){
        this.$axios.post(this.$httpUrl+'/rentgoods/listPage',{
          pageSize:this.pageSize,
          pageNum:this.pageNum,
          params:{
            rentgoodsName:this.rentgoodsName,
            cId: this.cId+''
          }
        }).then(res=>res.data).then(res=>{
          console.log(res)
          if (res.code==200){
            this.tableData=res.data
            this.total=res.total
          }else{
            alert("获取数据失败")
          }
        })
      }else{
        this.$axios.post(this.$httpUrl+'/rentgoods/listPage',{
          pageSize:this.pageSize,
          pageNum:this.pageNum,
          params:{
            rentgoodsName:this.rentgoodsName,
            cId: this.user.cid+''
          }
        }).then(res=>res.data).then(res=>{
          console.log(res)
          if (res.code==200){
            this.tableData=res.data
            this.total=res.total
          }else{
            alert("获取数据失败")
          }
        })
      }

    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    reset(){
      this.rentgoodsName=''
      this.cId=''
      this.loadPost()
    },
    doMod(){
      this.$axios.post(this.$httpUrl+'/rentgoods/update',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible=false
          this.loadPost()
        }else{
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    dosave(){
      if (this.user.roleId != '0'){
        this.form.cid = this.user.cid
        this.$axios.post(this.$httpUrl+'/rentgoods/add',this.form).then(res=>res.data).then(res=>{
          console.log(res)
          if (res.code==200){
            this.$message({
              showClose: true,
              message: '操作成功',
              type: 'success'
            });
            this.centerDialogVisible=false
            this.loadPost()
          }else{
            this.$message({
              showClose: true,
              message: '操作失败',
              type: 'error'
            });
          }
        })
      }else {
        this.$axios.post(this.$httpUrl + '/rentgoods/add', this.form).then(res => res.data).then(res => {
          console.log(res)
          if (res.code == 200) {
            this.$message({
              showClose: true,
              message: '操作成功',
              type: 'success'
            });
            this.centerDialogVisible = false
            this.loadPost()
          } else {
            this.$message({
              showClose: true,
              message: '操作失败',
              type: 'error'
            });
          }
        })
      }
    },
    addsave(){
      this.$axios.post(this.$httpUrl+'/rentgoods/addsave',this.addform).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          });
          this.addDialogVisible=false
          this.addform.rentgoodsCount = ''
          this.loadPost()
        }else{
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    save(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.rentgoodsId==''){
            console.log("新增")
            this.dosave();
          }else{
            console.log("修改")
            this.doMod();
            this.form.rentgoodsId=''
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    add(){
      this.centerDialogVisible=true
      this.disabled_index = false
      this.$nextTick(()=>{
        this.resetForm()
      })
    },
    add1(row){
      this.addDialogVisible=true
      this.$nextTick(()=>{
        this.addform.rentgoodsId= row.rentgoodsId
        this.addform.rentgoodsName= row.rentgoodsName
      })
    }
  }
}
</script>

<style scoped>

</style>